<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>查询</title>
    <style>
        .search {
            width: 178px;
            height: 200px;
            margin: 100px;
            position: relative;
        }

        .con {
            display: none;
            position: absolute;
            top: -40px;
            width: 170px;
            border: 1px solid #444444;
            box-shadow: 0 2px 4px #444444;
            padding: 5px 0;
            font-size: 18px;
            line-height: 20px;
            color: #333;
        }

        .con::before {
            content: '';
            width: 0;
            height: 0;
            position: absolute;
            top: 30px;
            left: 18px;
            border: 8px solid #000;
            border-style: solid dashed dashed;
            border-color: #fff transparent transparent;
        }
    </style>
</head>

<body>
    <div class="search">
        <div class="con"></div>
        <input type="text" class="jd" placeholder="please input">
    </div>
    <script>
        //使用键盘事件对象里面的keycode判断用户按下的是否是s键/搜索框获得焦点:使用js里面的focus()方法
        var jd_input = document.querySelector('input');
        var con = document.querySelector('.con');
        document.addEventListener('keyup', function (e) {
            console.log(e.keyCode);
            if (e.keyCode === 83) {
                jd_input.focus();
            }
        })
        jd_input.addEventListener('keyup', function () {
            if (this.value == '') {
                con.style.display = 'none'
            } else {
                con.style.display = 'block';
                con.innerHTML = this.value;
            }
        })
        //失去焦点时隐藏放大
        jd_input.addEventListener('blur',function(){
            con.style.display = 'none'
        })
          //获得焦点时显示盒子
        jd_input.addEventListener('focus',function(){
           if(this.value!==''){
            con.style.display = 'block';
           }
        })
    </script>
</body>

</html>